<template>
  <el-dialog
    title="新增员工"
    :visible="dialogVisible"
    @close="btnCancel"
  >
    <!-- 表单区域 -->
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="formData.username" placeholder="请输入姓名" style="width:50%" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="手机号" style="width:50%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" placeholder="入职时间" style="width:50%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" placeholder="工号" style="width:50%" />
      </el-form-item>

      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" placeholder="部门" style="width:50%" @focus="getDepartmentList" />
        <!-- 树结构 -->
        <div v-if="isShowTree" v-loading="loading" class="treeBox">
          <el-tree
            :data="treeData"
            :props="{label:'name'}"
            :default-expand-all="true"
            @node-click="treeNode"
          />
        </div>

      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker v-model="formData.correctionTime" placeholder="转正时间" style="width:50%" />
      </el-form-item>
    </el-form>
    <!-- 按钮区域 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button type="primary" size="small" @click="btnOk">确认</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum,
      // 表单内容
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单校验
      rules: {
        // 姓名
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }],
        // 手机号
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }],
        // 聘用形式
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        // 入职时间
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],
        // 工号、
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        // 部门
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        // 转正时间
        correctionTime: []
      },
      treeData: [],
      isShowTree: false, // 部门展示
      loading: false
    }
  },
  methods: {
    // 部门展示
    async getDepartmentList() {
      this.loading = true
      // 1. 拿部门数据
      const { depts } = await getDepartments()
      // 2.转换为属性数据
      this.treeData = transListToTreeData(depts, '')
      // 3.属性组件展示时机
      this.isShowTree = true
      this.loading = false
    },
    // 选择部门
    treeNode(node) {
      this.formData.departmentName = node.name
      this.isShowTree = false
    },
    // 确认按钮
    btnOk() {
      // 手动校验
      this.$refs.formData.validate(async isOk => {
        if (isOk) {
          // 后台数据调接口更新
          await addEmployee(this.formData)
          // 前端更新
          this.$emit('addFn')
          this.btnCancel()
          // =提示用户
          this.$message.success('添加成功')
        }
      })
    },
    // 取消
    btnCancel() {
      this.$refs.formData.resetFields()
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>

<style scoped>
.treeBox{
    width: 50%;
    height: 300px;
    overflow: auto;
}

</style>
